<?php
namespace app\common\model\User;

use app\common\model\Tag\GoodsTagRelationCommonModel;
use support\bootstrap\Cache;
use support\Db;
use support\Model;

/**
 * 用户商品爱好标签表
 * Class UserHobbyKeyCommonModel
 * @package app\common\model\User
 */
class UserHobbyTagCommonModel extends Model
{

    protected $name = 'user_hobby_tag';

    /**
     * 类型:1=商品标签
     */
    const TYPE_GOODS = 1;

    const LEVEL_ONE = 1;
    const LEVEL_TWO = 2;
    const LEVEL_THREE = 3;
    const LEVEL_FOUR= 4;

    const HOBBY_LEVEL = [
        self::TYPE_GOODS => [
            self::LEVEL_ONE,//查看
            self::LEVEL_TWO,//收藏
            self::LEVEL_THREE,//喜欢
            self::LEVEL_FOUR,//曾经购买过
        ]
    ];

    /**
     * 获取用户喜爱的标签
     * @param int $userId 用户id
     * @param int $level 优先等级
     * @param int $type 类型
     * @return array
     */
    public function getUserHobbyTag(int $userId , int $level = 0 , int $type = 0) : array
    {
        //1.先读出缓存
        $tagIds = $this->getUserHobbyTagCache($userId , $level , $type);

        //2.缓存不存在时，则去读表
        if(empty($tagIds)){
            $where = [
                'user_id' => $userId,
            ];
            $optionKey = [ 'level' , 'type' ];
            foreach($optionKey as $v){
                if(!empty($$v)){
                    $where[$v] = $$v;
                }
            }
            $tagIds = Db::name($this->name)->where($where)->column('tag_id');
            $this->setUserHobbyTagCache($tagIds , $userId , $level , $type );
        }
        return $tagIds;
    }

    /**
     * 保存用户的爱好标签
     * @param int $userId
     * @param int $goodsId
     * @param int $level
     * @param int $type
     * @throws \Exception
     */
    public function saveUserHobbyTagByGoodsId(int $userId , int $goodsId ,int $level = 1 , int $type = 1)
    {
        $goodsTagRelation = new GoodsTagRelationCommonModel();
        $tagIds = $goodsTagRelation->getTagIdsByGoodsId($goodsId);
        if(!empty($tagIds)){
            $saveData = [];
            foreach($tagIds as $v){
                $data = ['user_id' => $userId , 'level' => $level , 'type' => $type , 'tag_id' => $v];
                if(!$this->isExistRecord($data)){
                    $saveData[] = $data;
                }
                unset($data);
            }
            if(!empty($saveData)){
                //保存数据。删除缓存并重新设置爱好缓存
                $this->saveAll($saveData);
                $this->delUserHobbyTagCache($userId , $level , $type);
                $this->getUserHobbyTag($userId , $level , $type);
            }
        }
    }

    /**
     * 获取缓存
     * @param int $userId
     * @param int $level
     * @param int $type
     * @return mixed
     */
    public function getUserHobbyTagCache(int $userId , int $level , int $type)
    {
        return Cache::get( $this->getCacheKey($userId , $level , $type) , [] );
    }

    /**
     * 设置缓存
     * @param $value
     * @param int $userId
     * @param int $level
     * @param int $type
     * @param int $expire
     */
    public function setUserHobbyTagCache( $value , int $userId , int $level , int $type  , int $expire = 72000)
    {
        Cache::set( $this->getCacheKey($userId , $level , $type) , $value , $expire);
    }


    /**
     * 删除缓存
     * @param int $userId
     * @param int $level
     * @param int $type
     */
    public function delUserHobbyTagCache(int $userId , int $level , int $type )
    {
        Cache::delete( $this->getCacheKey($userId , $level , $type));
    }

    /**
     * 获取缓存key
     * @param int $userId
     * @param int $level
     * @param int $type
     * @return string
     */
    protected function getCacheKey(int $userId , int $level , int $type)
    {
        return 'userHobbyTag-' . implode('-' , [$userId , $level , $type]);
    }



}